package br.com.engine.componentes;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;

import br.com.engine.controle.ControleBase;
import br.com.engine.controle.Tela;
import br.com.engine.interfaces.IComponent;

public class SpriteFont implements IComponent
{
    public static final int BOLD   = Font.BOLD;
    public static final int PLAIN  = Font.PLAIN;
    public static final int ITALIC = Font.ITALIC;
    
	private String  texto        = "";
	private Point   location     = new Point( 0, 0 );
	private boolean centralizarX = false;
	private Color   color        = Color.BLACK;
	private int     fontSize     = 20;
	private int     fontType     = Font.PLAIN;
	
	private Graphics2D graphics;   

	public SpriteFont( )
	{
		this.graphics = ControleBase.getInstance( ).getGraphics2d( );
	}
	
	@Override
	public void setup( ) 
	{
		
	}

	@Override
	public void draw( ) 
	{
		Graphics2D g = (Graphics2D)this.graphics.create( );
		
		Font font = new Font( "Fonte", fontType, fontSize );
		
		g.setFont( font );
		
		g.setColor( getColor( ) );

		if( isCentralizarX( ) )
		{
			int stringWidth = g.getFontMetrics( ).stringWidth( getTexto( ) );
			g.drawString( getTexto( ), ( Tela.getTela( ).getWidth( ) / 2 ) - ( stringWidth / 2 ), getLocation().y + 15 );
		}
		else
			g.drawString( getTexto( ), getLocation( ).x, getLocation( ).y + 15 );
		
		g.dispose( );
	}	
	
	public String getTexto( )
	{
		return texto;
	}

	public void setTexto( String texto )
	{
		this.texto = texto;
	}

	public Point getLocation( )
	{
		return location;
	}

	public void setLocation( Point location )
	{
		this.location = location;
	}
	
	public void setLocation( int x, int y )
    {
        setLocation( new Point( x, y ) );
    }
	
	public boolean isCentralizarX( )
	{
		return centralizarX;
	}
	
	public void setCentralizarX( boolean centralizarX )
	{
		this.centralizarX = centralizarX;
	}
	
	public void setLocationYCenterX( int y, boolean centerX )
	{
		setCentralizarX( centerX );
		setLocation    ( 0, y    );
	}
	
	public Color getColor( ) 
	{
		return color;
	}

	public void setColor( Color color )
	{
		this.color = color;
	}
	
	public int getFontSize( )
	{
		return fontSize;
	}
	
	public void setFontSize( int fontSize )
	{
		this.fontSize = fontSize;
	}
	
	public void setFontType( int fontTipe )
    {
	    this.fontType = fontTipe;
    }
	
	public static void adjustText( GameObject obj, String texto )
	{
		SpriteFont font = obj.getComponent( SpriteFont.class );
		
		font.setTexto( texto );
	}

    public void createGraphics( Graphics2D graphics )
    {
        this.graphics = graphics;
    }
}